package com.cjm.one.order.admin.controller;

import com.cjm.one.common.result.Result;
import com.cjm.one.model.base.PageVo;
import com.cjm.one.model.order.dto.OrderChartItem;
import com.cjm.one.model.order.dto.OrderNoRefundDto;
import com.cjm.one.model.order.enums.OrderStatusEnum;
import com.cjm.one.model.order.enums.PayTypeEnum;
import com.cjm.one.model.order.vo.OrderInfoVo;
import com.cjm.one.order.service.OrderInfoService;
import com.cjm.one.order.service.StatsService;
import com.cjm.one.security.annotation.Within;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "后台订单信息")
@RequestMapping("/admin/order/info")
public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private StatsService statsService;


    @GetMapping("/list")
    @Operation(summary = "订单列表")
    public Result<PageVo<OrderInfoVo>> list(
            @RequestParam(value = "current", required = true, defaultValue = "1") Long current,
            @RequestParam(value = "size", required = true, defaultValue = "10") Long size,
            @RequestParam(value = "userId",required = false) Long userId,
            @RequestParam(value = "orderNo",required = false) String orderNo,
            @RequestParam(value ="status",required = false) OrderStatusEnum status,
            @RequestParam(value ="payStatus",required = false) PayTypeEnum payStatus,
            @RequestParam(value ="userName",required = false) String userName,
            @RequestParam(value ="createTimeStart",required = false) String createTimeStart,
            @RequestParam(value ="createTimeEnd",required = false) String createTimeEnd,
            @RequestParam(value ="phone",required = false) String phone
    ) {

        return Result.ok(orderInfoService.pageList(current, size, userId, orderNo, status, payStatus, userName, createTimeStart, createTimeEnd, phone));
    }
    @GetMapping("/{id}")
    @Operation(summary = "订单详情")
//    @Within  @RequestHeader(SecurityConstants.FROM_SOURCE) String fromSource
    public Result<OrderInfoVo> getOrderInfo(@PathVariable("id") Long id) {

        return Result.ok(orderInfoService.getOrderInfoById(id));
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单")
    public Result<Boolean> delete(@PathVariable("id") Long id) {

        return Result.ok(orderInfoService.deleteOrderById(id));
    }
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除订单")
    public Result<Boolean> deleteBatch(@RequestParam("ids") Long[] ids) {
        return Result.ok(orderInfoService.deleteBatchOrderByIds(ids));
    }
    @PostMapping("/refund/{id}")
    @Operation(summary = "退款")
    public Result<Boolean> refund(@PathVariable("id") Long id) {
        //TODO 退款
        return Result.ok(orderInfoService.refundOrderById(id));
    }
    @PutMapping("/status/{id}")
    @Operation(summary = "修改订单状态")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @RequestParam("status") OrderStatusEnum status) {
        log.info("修改订单状态,id:{},status:{}", id, status);
        return Result.ok(orderInfoService.updateOrderStatusById(id, status));
    }
    @PutMapping("/noRefund")
    @Operation(summary = "拒绝退款接口")
    public Result<Boolean> noRefund(@RequestBody OrderNoRefundDto orderNoRefundDto) {
        //TODO 拒绝退款
        return Result.ok(orderInfoService.noRefundOrderByNo(orderNoRefundDto));
    }

    @GetMapping("/test")
    public Result<OrderStatusEnum> test() {

        return Result.ok(OrderStatusEnum.CANCEL);
    }
    // 获取顶部统计卡片数据
    @GetMapping("/summary")
    @Operation(summary = "获取顶部统计卡片数据")
    public Result<Map<String, Object>> getOrderSummary(
            @RequestParam(defaultValue = "30d") String timeRange) {
        return Result.ok(statsService.getOrderSummary(timeRange));
    }

    // 获取图表数据
    @GetMapping("/chart")
    @Operation(summary = "获取图表数据")
    public Result<List<OrderChartItem>> getOrderChart(
            @RequestParam(defaultValue = "30d") String timeRange) {
        return Result.ok(statsService.getOrderChartData(timeRange));
    }
    @Within
    @GetMapping("/countPaidUsers")
    @Operation(summary = "统计支付用户数")
    public Result<Long> countPaidUsers(@RequestParam(name = "startTime") LocalDateTime startTime, @RequestParam(name = "endTime") LocalDateTime endTime) {
        return Result.ok(orderInfoService.countPaidUsers(startTime,endTime));
    }


}
